package org.example;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.coyote.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {

    /**
     * 方式一：使用HttpServletResponse设置响应数据
     */
    @RequestMapping("/response")
    public void response(HttpServletResponse response) throws IOException {
        //1.设置响应状态码
        response.setStatus(401);
        //2.设置响应头
        response.setHeader("name","zhangsan");
        //3.设置响应体
        response.getWriter().write("<h1>hello world</h1>");

    }

    /**
     * 方式二：靶想要响应数据的封装成ResponseEntity对象，直接返回
     */
    @RequestMapping("/response2")
    public ResponseEntity<String> response2() {
        return ResponseEntity.status(401).header("name","zhangsan").body("<h1>hello world</h1>");
    }
}
